package com.totoro.utils;

import lombok.SneakyThrows;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;

/**
 * @author TOTORO
 * @date 2021/9/3 8:26
 * 手机验证码工具类
 */
public class PhoneUtil {

    @SneakyThrows
    public static String getPhoneCode(String phone, int phoneCode) {
        try {
            HttpClient client = new HttpClient();
            PostMethod post = new PostMethod("http://gbk.api.smschinese.cn");
            post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");
            NameValuePair[] data = { new NameValuePair("Uid", "Totoro1"),
                    new NameValuePair("Key", "sndmwkendnakwlrnfba17"),
                    new NameValuePair("smsMob", phone),
                    new NameValuePair("smsText", "您的验证码： "+phoneCode+"，请牢记，勿泄露给他人！") };
            post.setRequestBody(data) ;
            client.executeMethod(post);
            Header[] headers = post.getResponseHeaders();
            int statusCode = post.getStatusCode();
            String result = new String(post.getResponseBodyAsString().getBytes(
                    "gbk"));
            post.releaseConnection();

            //判断提示
            if(result.equals("-1")) {
                // 并没有该手机用户...
            } else if(result.equals("-11")){
                // 该手机用户已被禁用
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return "";
    }

}
